package top.z.work.framework.common.util;

import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public final class IdWorkerUtil {

    private IdWorkerUtil() {
    }

    private final static Map<String, Snowflake> idWorkerMap = new ConcurrentHashMap<>();

    synchronized
    public static Snowflake idWorker(int workerId, int datacenterId) {
        Snowflake idWorker = idWorkerMap.get(workerId + "" + datacenterId);
        if (idWorker == null) {
            idWorker = IdUtil.getSnowflake(workerId, datacenterId);
            idWorkerMap.put(workerId + "" + datacenterId, idWorker);
        }
        return idWorker;
    }

    synchronized
    public static Snowflake idWorker() {
        return IdWorkerUtil.idWorker(1, 1);
    }

    public static Long nid() {
        return IdWorkerUtil.idWorker().nextId();
    }

    public static String sid() {
        return String.valueOf(IdWorkerUtil.idWorker().nextId());
    }

}
